/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.net.SocketException;
import java.net.UnknownHostException;
import stendcontrol.CoefConfigurator;
import stendcontrol.Configurator;
import stendcontrol.DataProcessor;
import stendcontrol.Experiment;
import stendcontrol.ExperimentContainer;
import stendcontrol.MessageBuilder;
import stendcontrol.Param;
import stendcontrol.SensorInformer;
import stendcontrol.StendControlAPI;
import stendcontrol.UdpImplementer;
import stendcontrol.UdpServer;

/**
 *
 * @author minaev
 */
public class StendControlAPI_Test {

    static ExperimentContainer exp_cont = new ExperimentContainer();

    public static void main(String[] args) throws SocketException,
            UnknownHostException, InterruptedException {
        Configurator cnf = Configurator.getConf();
        //UDP_Fluder udp_flud = new UDP_Fluder("127.0.0.255", 1);
        UdpServer udp_serv = new UdpServer("192.168.0.131",
                (Integer) cnf.getProperty(Configurator.UDP_IN_PORT),
                (Integer) cnf.getProperty(Configurator.UDP_OUT_PORT));
        MessageBuilder mb = new MessageBuilder();
        DataProcessor dp = new DataProcessor();
        Experiment exp = new Experiment();
        final SensorInformer si = new SensorInformer(
                CoefConfigurator.getConf().getSensorCoef0(),
                CoefConfigurator.getConf().getSensorCoefProp());
        UdpImplementer ui = new UdpImplementer(udp_serv);
        StendControlAPI sca = new StendControlAPI(ui, si);

        //Thread t_udp_flud = new Thread(udp_flud);
        Thread t_udp_serv = new Thread(udp_serv);
        Thread t_mb = new Thread(mb);

        /*class Spaning implements Runnable {

         boolean stop = false;

         public void stopSpan() {
         stop = true;
         }

         @Override
         public void run() {
         span1.addValue(4096);
         span2.addValue(4096);
         span3.addValue(4096);

         while (!stop) {
         span1.addValue(si.getScaleSensor(0));
         span2.addValue(si.getScaleSensor(1));
         span3.addValue(si.getScaleSensor(2));
         try {
         Thread.sleep(10);
         } catch (InterruptedException ex) {
         Logger.getLogger(Experiment_Test.class.getName()).log(Level.SEVERE, null, ex);
         }
         }
         }
         };*/

        /*Spaning span = new Spaning();
         Thread t_span = new Thread(span);*/

        udp_serv.addTransmitListener(mb);
        udp_serv.addTransmitListener(si);
        mb.addMessageListener(exp);
        mb.addMessageListener(sca);

        t_mb.start();

        udp_serv.startTransmitToExp();
        t_udp_serv.start();

        //t_udp_flud.start();

        Thread.sleep(2000);

        //udp_flud.shutdown();
        udp_serv.stopUDPThread();
        mb.stopBuilding();

        //System.out.println("flud wait...");
        //t_udp_flud.join();
        System.out.println("serv wait...");
        t_udp_serv.join();
        System.out.println("mb wait...");
        t_mb.join();


        exp.close();

        while (!exp.isClosed()) {
        }


        /*
         MeasureSeqMessage msm = exp.getFirstMeasureSeq();
         int k = 0;
            
            
         while (msm != null) {
         System.out.println("MeasSeq size:" + msm.getSize());
         /*for (int i = 0; i < msm.getSize(); i++) {
         System.out.println("sensor val:"+msm.getSensorValue(0, i)+":"+proc0[k]);
         k++;
         }*//*
         msm = msm.getNextMeasureSeq();
         }
         */
        /*pan.addLinePlot(null, Color.RED, exp.getProcess(0));
         pan.addLinePlot(null, Color.GREEN, exp.getProcess(1));
         pan.addLinePlot(null, Color.BLUE, exp.getProcess(2));*/

        System.out.println("gen");
        exp.getPositions();
        for (int i = 0; i < Configurator.COUNT_SENSOR; i++) {
            dp.setProcess(exp.getProcess(i), exp.getPositions());
            exp.addParam(new Param("M", Param.P_MEAN, dp.getMean()),
                    i);
        }
        System.out.println("end gen");

        exp.setExpName("Experiment" + exp_cont.getSize());
        exp_cont.addExp(exp);

        cnf.saveConfig();
    }
}
